﻿
public class Solution88
{
    public void Merge1(int[] nums1, int m, int[] nums2, int n)
    {


        int index1 = 0;
        int index2 = 0;

        while (index1 < m && index2 < n)
        {

            Console.WriteLine(index1);
            if (nums1[index1] < nums2[index2])
            {
                index1++;


            }
            else
            {


                Move(nums1, index1);
                nums1[index1] = nums2[index2];
                index1++;
                index2++;
                m++;
            }



        }

        while (index1 < m + n && index2 < n)
        {

            nums1[index1] = nums2[index2];
            index1++;
            index2++;
        }

    }

    private void Move(int[] nums, int start)//从start开始后移一位
    {
        for (int i = nums.Length - 1; i > start; i--)
        {

            nums[i] = nums[i - 1];

        }
    }
    //从后向前遍历
    public void Merge(int[] nums1, int m, int[] nums2, int n)
    {


        int index1 = m - 1;
        int index2 = n - 1;
        int tail = m + n - 1;//num1的尾巴

        while (index1 >= 0 && index2 >= 0)
        {


            if (nums1[index1] < nums2[index2])
            {
                nums1[tail--] = nums2[index2--];
            }
            else
            {
                nums1[tail--] = nums1[index1--];
            }

        }

       
        if (index1 < 0)
        {
            while (index2 >= 0)
            {
                nums1[tail--] = nums2[index2--];
            }

        }
        if (index2 < 0)
        {
            while (index1 >= 0)
            {
                nums1[tail--] = nums1[index1--];
            }

        }

    }
}


class Program88
{

    static void Main88(string[] args)
    {
        //int[] nums1 = [1, 2, 3, 0, 0, 0], nums2 = [2, 5, 6];
        int[] nums1 = [ 0], nums2 = [1];
        //int m = 3, n = 3;
        int m = 0, n = 1;
        Solution88 solution = new Solution88();
        solution.Merge(nums1, m, nums2, n);
    }



}